package org.jeecg.modules.index;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.hxrencai.bo.NumBo;
import org.jeecg.modules.hxrencai.bo.SchoolBo;
import org.jeecg.modules.hxrencai.bo.ThreeDataBo;
import org.jeecg.modules.hxrencai.dto.MergeDto;
import org.jeecg.modules.hxrencai.entity.StudyPark;
import org.jeecg.modules.hxrencai.entity.University;
import org.jeecg.modules.hxrencai.service.IStudyParkService;
import org.jeecg.modules.hxrencai.service.IUniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
//@Api(tags = "首页接口")
//@RestController
//@RequestMapping("/hxrencai/index")
public class IndexController {
    @Autowired
    private IUniversityService universityService;
    @Autowired
    private IStudyParkService studyParkService;

    /**
     * 分页列表查询
     *
     * @param req
     * @return
     */
//    @ApiOperation(value = "分页列表查询  导入类型 1 福建高职 2 福建本科 3 全国园区 4 全国研究  5 全国本科  6 福建中职 ", notes = "分页列表查询")
//    @GetMapping(value = "/list")
    public Result<?> queryPageList(MergeDto dto,
                                   HttpServletRequest req) {
        int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo();
        int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();
        if (StringUtils.isBlank(dto.getImportType())) {
            return Result.error("导入类型 1 福建高职 2 福建本科 3 全国园区 4 全国研究  5 全国本科  6 福建中职");
        }
        University university = new University();
        StudyPark studyPark = new StudyPark();
        Page<University> page1 = new Page<University>(pageNo, pageSize);
        Page<StudyPark> page2 = new Page<StudyPark>(pageNo, pageSize);
        if ("5".equals(dto.getImportType())) {
            BeanUtil.copyProperties(dto, university);
            LambdaQueryWrapper<University> queryWrapper1 = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(university.getImportType())) {
                queryWrapper1.eq(University::getImportType, dto.getImportType());
            }
            if (StringUtils.isNotBlank(dto.getArea())) {
                String area = dto.getArea();
                if (!area.endsWith("市")) {
                    area = area.substring(0, area.length() - 1);
                }
                queryWrapper1.like(University::getArea, area);
            }
            IPage<University> pageList1 = universityService.page(page1, queryWrapper1);
            return Result.ok(pageList1);
        } else if ("1".equals(dto.getImportType()) || "2".equals(dto.getImportType()) || "6".equals(dto.getImportType())) {
            BeanUtil.copyProperties(dto, university);
            LambdaQueryWrapper<University> queryWrapper2 = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(university.getImportType())) {
                queryWrapper2.eq(University::getImportType, dto.getImportType());
            }
            if (StringUtils.isNotBlank(dto.getArea())) {
                String area = dto.getArea();
                if (area.endsWith("市")) {
                    area = area.substring(0, area.length() - 1);
                }
                queryWrapper2.like(University::getArea, area);
            }
            IPage<University> pageList2 = universityService.page(page1, queryWrapper2);
            return Result.ok(pageList2);
        } else if ("3".equals(dto.getImportType()) || "4".equals(dto.getImportType())) {
            BeanUtil.copyProperties(dto, studyPark);
            //QueryWrapper<StudyPark> queryWrapper = QueryGenerator.initQueryWrapper(studyPark, req.getParameterMap());
            LambdaQueryWrapper<StudyPark> queryWrapper3 = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(studyPark.getImportType())) {
                queryWrapper3.eq(StudyPark::getImportType, dto.getImportType());
            }
            if (StringUtils.isNotBlank(dto.getArea())) {
                queryWrapper3.like(StudyPark::getArea, dto.getArea());

            }
            IPage<StudyPark> pageList3 = studyParkService.page(page2, queryWrapper3);
            return Result.ok(pageList3);
        } else {
            return Result.error("导入类型 1 福建高职 2 福建本科 3 全国园区 4 全国研究  5 全国本科  6 福建中职");
        }
    }

    /**
     * 列表查询
     *
     * @param req
     * @return
     */
//    @ApiOperation(value = "列表查询  导入类型 1 福建高职 2 福建本科 3 全国园区 4 全国研究  5 全国本科  6 福建中职 ", notes = "列表查询")
//    @GetMapping(value = "/queryList")
    public Result<?> queryList(University dto,
                               HttpServletRequest req) {
        if (StringUtils.isBlank(dto.getImportType())) {
            return Result.error("导入类型 1 福建高职 2 福建本科 3 全国园区 4 全国研究  5 全国本科  6 福建中职");
        }
        University university = new University();
        StudyPark studyPark = new StudyPark();
        if ("5".equals(dto.getImportType())) {
            BeanUtil.copyProperties(dto, university);
            LambdaQueryWrapper<University> queryWrapper1 = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(university.getImportType())) {
                queryWrapper1.eq(University::getImportType, dto.getImportType());
            }
            if (StringUtils.isNotBlank(dto.getArea())) {
                String area = dto.getArea();
                if (!area.endsWith("市")) {
                    area = area.substring(0, area.length() - 1);
                }
                queryWrapper1.like(University::getArea, area);
            }
            List<University> list = universityService.list(queryWrapper1);
            return Result.ok(list);
        } else if ("1".equals(dto.getImportType()) || "2".equals(dto.getImportType()) || "6".equals(dto.getImportType())) {
            BeanUtil.copyProperties(dto, university);
            LambdaQueryWrapper<University> queryWrapper2 = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(university.getImportType())) {
                queryWrapper2.eq(University::getImportType, dto.getImportType());
            }
            if (StringUtils.isNotBlank(dto.getArea())) {
                String area = dto.getArea();
                if (area.endsWith("市")) {
                    area = area.substring(0, area.length() - 1);
                }
                queryWrapper2.like(University::getArea, area);
            }
            List<University> list = universityService.list(queryWrapper2);
            return Result.ok(list);
        } else if ("3".equals(dto.getImportType()) || "4".equals(dto.getImportType())) {
            BeanUtil.copyProperties(dto, studyPark);
            LambdaQueryWrapper<StudyPark> queryWrapper3 = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(studyPark.getImportType())) {
                queryWrapper3.eq(StudyPark::getImportType, dto.getImportType());
            }
            if (StringUtils.isNotBlank(dto.getArea())) {
                queryWrapper3.like(StudyPark::getArea, dto.getArea());
            }
            List<StudyPark> list2 = studyParkService.list(queryWrapper3);
            return Result.ok(list2);
        } else {
            return Result.error("导入类型 1 福建高职 2 福建本科 3 全国园区 4 全国研究  5 全国本科  6 福建中职");
        }
    }


    //    @ApiOperation(value = "获取本科院校省份前三", notes = "获取本科院校省份前三")
//    @GetMapping(value = "/getFrontThreeByProvince")
    public List<NumBo> getFrontThreeByProvince(@RequestParam String province, @RequestParam(required = false) String city) {
        if (StringUtils.isBlank(city)) {
            List<NumBo> list = universityService.getFrontThreeByProvince(province);
            return list;
        } else {
            if (city.endsWith("市")) {
                city = city.substring(0, city.length() - 1);
            }
            LambdaQueryWrapper<University> qw = new LambdaQueryWrapper();
            qw.eq(University::getImportType, "2");
            qw.like(University::getArea, city);
            List<University> universities = universityService.list(qw);
            Set<String> strings = new HashSet<>();
            List<NumBo> list = new ArrayList<>();
            for (University zz : universities) {
                NumBo numBo = null;
                if (!strings.contains(zz.getSchoolName())) {
                    strings.add(zz.getSchoolName());
                    numBo = new NumBo();
                    numBo.setName(zz.getSchoolName());
                    if (StringUtils.isNotBlank(zz.getProfessionNum())) {
                        int num = 0;
                        num += Integer.valueOf(zz.getProfessionNum());
                        numBo.setNum(num + "");
                    }
                    list.add(numBo);
                } else {
                    for (NumBo numBo1 : list) {
                        if (numBo1.getName().equals(zz.getSchoolName())) {
                            numBo = numBo1;
                            if (StringUtils.isNotBlank(zz.getProfessionNum())) {
                                numBo.setNum(Integer.valueOf(numBo.getNum()) + Integer.valueOf(zz.getProfessionNum()) + "");
                            }
                        }
                    }
                }
            }
            list.sort(new Comparator<NumBo>() {
                @Override
                public int compare(NumBo o1, NumBo o2) {
                    return Integer.valueOf(o2.getNum()) - Integer.valueOf(o1.getNum());
                }
            });
            return list.stream().limit(3).collect(Collectors.toList());
        }

    }


    //    @ApiOperation(value = "根据省份获取 研究院 本科 园区 数量 ", notes = "根据省份获取 研究院 本科 园区 数量")
//    @GetMapping(value = "/getHomeThreeData")
    public ThreeDataBo getHomeThreeData(@RequestParam String province, @RequestParam(required = false) String city) {
        ThreeDataBo threeDataBo = null;
        if (StringUtils.isNotBlank(province) && province.startsWith("福建")) {
            if (StringUtils.isBlank(city)) {
                //查询 省的
                threeDataBo = new ThreeDataBo();
                ThreeDataBo schoolBo = universityService.queryFjTotalNum();
                threeDataBo.setBenke(schoolBo.getBenke());
                threeDataBo.setBkNum(schoolBo.getBkNum());
            } else {
                //查询地区
                threeDataBo = new ThreeDataBo();
                LambdaQueryWrapper<University> qw = new LambdaQueryWrapper();
                //本科
                qw.eq(University::getImportType, "2");
                qw.like(University::getArea, city);
                List<University> benke = universityService.list(qw);
                //高职
                qw = new LambdaQueryWrapper();
                qw.eq(University::getImportType, "1");
                qw.like(University::getArea, city);
                List<University> gaozhi = universityService.list(qw);
                //中职
                qw = new LambdaQueryWrapper();
                qw.eq(University::getImportType, "6");
                qw.like(University::getArea, city);
                List<University> zhongzhi = universityService.list(qw);
                Set<String> strings = new HashSet<>();
                int num = 0;
                //本科
                for (University bk : benke) {
                    if (!strings.contains(bk.getSchoolName())) {
                        strings.add(bk.getSchoolName());
                    }
                    if (StringUtils.isNotBlank(bk.getProfessionNum())) {
                        num += Integer.valueOf(bk.getProfessionNum());
                    }
                }
                threeDataBo.setBenke(strings.size());
                threeDataBo.setBkNum(num);
                //高职
                strings = new HashSet<>();
                num = 0;
                for (University gz : gaozhi) {
                    if (!strings.contains(gz.getSchoolName())) {
                        strings.add(gz.getSchoolName());
                    }
                    if (StringUtils.isNotBlank(gz.getProfessionNum())) {
                        num += Integer.valueOf(gz.getProfessionNum());
                    }
                }
                threeDataBo.setGaozhi(strings.size());
                threeDataBo.setGaozhiNum(num);
                //中职
                strings = new HashSet<>();
                num = 0;
                for (University zz : zhongzhi) {
                    if (!strings.contains(zz.getSchoolName())) {
                        strings.add(zz.getSchoolName());
                    }
                    if (StringUtils.isNotBlank(zz.getProfessionNum())) {
                        num += Integer.valueOf(zz.getProfessionNum());
                    }
                }
                threeDataBo.setZhongzhi(strings.size());
                threeDataBo.setZhongzhiNum(num);
            }

        } else {
            //全国的  除去福建
            threeDataBo = universityService.queryProvinceTotalBknum(province);
        }

        if (ObjectUtil.isEmpty(threeDataBo)) {
            threeDataBo = new ThreeDataBo();
            threeDataBo.setYanjiu(0);
            threeDataBo.setYuanqu(0);
            threeDataBo.setBenke(0);
            threeDataBo.setBkNum(0);
            threeDataBo.setGaozhi(0);
            threeDataBo.setGaozhiNum(0);
            threeDataBo.setZhongzhi(0);
            threeDataBo.setZhongzhiNum(0);
        }
        threeDataBo.setYuanqu(studyParkService.getNumByProvince(province, "3"));
        threeDataBo.setYanjiu(studyParkService.getNumByProvince(province, "4"));

        if (threeDataBo.getGaozhi() == null) {
            threeDataBo.setGaozhi(0);
        }
        if (threeDataBo.getGaozhiNum() == null) {
            threeDataBo.setGaozhiNum(0);
        }

        if (threeDataBo.getZhongzhi() == null) {
            threeDataBo.setZhongzhi(0);
        }
        if (threeDataBo.getZhongzhiNum() == null) {
            threeDataBo.setZhongzhiNum(0);
        }

        if (threeDataBo.getBenke() == null) {
            threeDataBo.setBenke(0);
        }
        if (threeDataBo.getBkNum() == null) {
            threeDataBo.setBkNum(0);
        }

        if (threeDataBo.getYanjiu() == null) {
            threeDataBo.setYuanqu(0);
        }

        if (threeDataBo.getYanjiu() == null) {
            threeDataBo.setYanjiu(0);
        }
        return threeDataBo;
    }


    //    @ApiOperation(value = "获取福建  本科  高职  中职 院校  数量 ", notes = "获取福建  本科  高职  中职 院校  数量 ")
//    @GetMapping(value = "/queryFjThreeNum")
    public SchoolBo queryFjThreeNum() {
        return universityService.queryFjThreeNum();
    }


    //    @ApiOperation(value = "全国省份人数排名", notes = "全国省份人数排名")
//    @GetMapping(value = "/getProvinceRank")
    public List<NumBo> getProvinceRank(@RequestParam(required = false) String profession) {
        List<NumBo> list = universityService.getProvinceRank(profession);
        list = list.stream().filter(e -> Integer.valueOf(e.getNum()) != 0).collect(Collectors.toList());
        return list;
    }


}
